package com.scau.wrongtitle.controller;

import com.scau.wrongtitle.bean.*;
import com.scau.wrongtitle.domin.UserInfo;
import com.scau.wrongtitle.service.UserInfoService;
import com.scau.wrongtitle.service.WrongTitleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api")
@CrossOrigin
public class WrongTitleController {
    private final UserInfoService userInfoService;
    private final WrongTitleService wrongTitleService;

    @Autowired
    public WrongTitleController(UserInfoService userInfoService, WrongTitleService wrongTitleService) {
        this.userInfoService = userInfoService;
        this.wrongTitleService = wrongTitleService;
    }

    @ApiOperation(value = "获取用户年级信息")
    @ApiImplicitParam(name = "id", value = "用户ID")
    @GetMapping("userInfo")
    public UserIndexInfo getUserIndexInfo(long id){
        UserInfo userInfo = userInfoService.getUserInfoById(id);
        return new UserIndexInfo(id,userInfo.getGrade(),userInfo.getWrongTitleCount(), userInfoService.getUserGrades(id));
    }

    @ApiOperation(value = "获取用户科目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID"),
            @ApiImplicitParam(name = "grade", value = "选择的年级")
    })
    @GetMapping("userSubject")
    public List<UserSubjectInfo> getUserSubjectInfo(long id, String grade){
        return userInfoService.getUserSubject(id,grade);
    }

    @ApiOperation(value = "获取用户科目错题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID"),
            @ApiImplicitParam(name = "grade", value = "选择的年级"),
            @ApiImplicitParam(name = "subject", value = "选择的科目")
    })
    @GetMapping("userSubjectTitle")
    public List<WrongTitleBean> getTitle(long id, String grade, String subject){
        return wrongTitleService.getUserTitle(id, grade, subject);
    }

    @ApiOperation(value = "设置收藏题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户ID"),
            @ApiImplicitParam(name = "qid", value = "题目ID"),
            @ApiImplicitParam(name = "isFavorite", value = "是否收藏")
    })
    @PostMapping("setFavorite")
    public void setFavorite(@RequestParam("uid")long uid, @RequestParam("qid")long[] qid, @RequestParam("isFavorite")boolean[] is){
        for(int i = qid.length - 1; i >= 0; i--)
            wrongTitleService.setFavorite(uid, qid[i], is[i]);
    }

    @ApiOperation(value = "设置攻克题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户ID"),
            @ApiImplicitParam(name = "qid", value = "题目ID"),
            @ApiImplicitParam(name = "isOvercome", value = "是否攻克")
    })
    @PostMapping("setOvercome")
    public void setOvercome(@RequestParam("uid")long uid, @RequestParam("qid")long[] qid, @RequestParam("isOvercome")boolean[] is){
        for(int i = qid.length - 1; i >= 0; i--)
            wrongTitleService.setOvercome(uid, qid[i], is[i]);
    }

    @ApiOperation(value = "删除已攻克题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户ID"),
            @ApiImplicitParam(name = "qid", value = "题目ID"),
            @ApiImplicitParam(name = "isDel", value = "是否删除")
    })
    @PostMapping("setDel")
    public void setDel(@RequestParam("uid")long uid, @RequestParam("qid")long[] qid, @RequestParam("isDel")boolean[] is){
        for(int i = qid.length - 1; i >= 0; i--)
            wrongTitleService.setDel(uid, qid[i], is[i]);
    }


    @ApiOperation(value = "添加错题题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户ID"),
            @ApiImplicitParam(name = "qid", value = "题目ID"),
            @ApiImplicitParam(name = "original", value = "来源")
    })
    @PostMapping("addWrongTitle")
    public WrongTitleBean addWrongTitle(@RequestParam("uid")long uid, @RequestParam("qid")long qid, @RequestParam("original")String original){
        wrongTitleService.addNewRecord(uid, qid, original);
        return wrongTitleService.test(uid, qid);
    }
}
